// 模拟量
var PageAin = {
    id: "PageAin", //页面id
    menu: "i18n.lan.menu.ain", //所属菜单(一级菜单名)
    title: "i18n.lan.menu.PageAin", //标题（二级菜单名）
    parentIcon: "icon-me-ain", //图标
    icon: "icon-me-ain", //图标
    refreshBtnCmd: "AIN_DUMPS", //刷新按钮指令
    saveBtn: true, //保存按钮
    el: [ //控件
        {//计算器弹出窗口
            elname: "div",
            attrs: {
                id: "calcWindow",
                class: "easyui-window",
                title: i18n.lan.PageAin.calcTip,
                style: "width:320px;height:180px;",
                closed: true,//默认不显示
            }
        },
        {//计算器图标
            elname: "div",
            attrs: {
                id: "ainCalc", title: i18n.lan.PageAin.calcTip, onclick: "creaetCalcWindow()",
            }
        },
        {
            elname: "datagrid",
            rowCount: Device.getNodesCount("ain"),
            data: {
                width: "830",
                frozenColumns: [
                    [{
                        //序号
                        field: 'index',
                        title: i18n.lan.common.index,
                        width: 40,
                        align: "center",
                    },]
                ],
                columns: [
                    [{//值
                        field: 'value',
                        width: 70,
                        dumpKey: "AIN<n>STATE",
                        dumpParamIndex: 1
                    },
                    {//名称
                        field: 'name',
                        editor: Easyui.editor.textbox,
                        width: 100,
                        dumpKey: "AIN<n>NAME",
                        cmdKey: "AIN<n>NAME",
                        dumpParamIndex: 0,
                        otherFields: "unit",
                        transcode: true
                    },
                    {//单位
                        field: "unit",
                        width: 60,
                        dumpKey: "AIN<n>NAME",
                        editor: Easyui.editor.textbox,
                        dumpParamIndex: 1,
                        transcode: true,
                        id: "ain<n>unit",
                    },
                    {//物理低值
                        field: "rangeLowVal",
                        editor: Easyui.editor.numberbox4,
                        dumpKey: "AIN<n>NODE_RANGE2",
                        dumpParamIndex: 1,
                        cmdKey: "AIN<n>RANGELOW",
                        width: 80,
                    },
                    {//物理高值
                        field: "rangeHighVal",
                        editor: Easyui.editor.numberbox2,
                        dumpKey: "AIN<n>NODE_RANGE2",
                        dumpParamIndex: 0,
                        cmdKey: "AIN<n>RANGEHIGH",
                        width: 80,
                    },
                    {//调整值
                        field: "adjValue",
                        width: 70,
                        dumpKey: "AIN<n>NODE_RANGE1",
                        editor: Easyui.editor.numberbox4,
                        editorType: 'numberbox',
                        dumpParamIndex: 3,
                        cmdKey: "AIN<n>ADJ"
                    },
                    {//转换比例
                        field: "scaleValue",
                        width: 70,
                        dumpKey: "AIN<n>NODE_RANGE1",
                        editor: Easyui.editor.numberbox4,
                        dumpParamIndex: 4,
                        cmdKey: "AIN<n>SCALE"
                    },
                    {//低通值
                        field: "filterLow",
                        width: 70,
                        dumpKey: "AIN<n>NODE_RANGE2",
                        editor: Easyui.editor.numberbox4,
                        dumpParamIndex: 4,
                        cmdKey: "AIN<n>FILTERLOW"
                    },
                    {//小数位数
                        field: "floatDots",
                        width: 70,
                        dumpKey: "AIN<n>NODE_TYPE",
                        editor: Easyui.editor.numberbox,
                        dumpParamIndex: 1,
                        cmdKey: "AIN<n>FLOATDOTS"
                    },
                    {//传感器类型
                        field: "sensorType",
                        formatter: Easyui.formatter.combobox,
                        editor: new Easyui.editor.Combobox(i18n.lan.PageAin.sensorTypeItems),
                        dumpKey: "AIN<n>NODE_TYPE",
                        dumpParamIndex: 0,
                        cmdKey: "AIN<n>TYPE",
                        width: 100,
                    },
                    ]
                ]
            },
        },
        
    ],
}
CmdPage.add(PageAin); //添加到页面管理

//创建计算器窗口
function creaetCalcWindow() {
    let html = '<div>\
        <span>{range}:</span>\
        <input type="text" value="4" id="ainWin_r0"> - \
        <input type="text" value="20" id="ainWin_r1">\
        </div>\
        <div><span>{actualRange}:</span>\
        <input type="text" id="ainWin_r2"> - \
        <input type="text" id="ainWin_r3"></div>\
        <div style="padding:5px;text-align:center;">\
        <button onclick=\'calcAdjAndScale();$("#calcWindow").window("close");\'>{ok}</button>\
        <button onclick=\'$("#calcWindow").window("close")\'>{cancel}</button>\
        </div>'.format(i18n.lan.PageAin);
    let win = $("#calcWindow");
    win.html(html);
    win.window({ collapsible: false, minimizable: false, maximizable: false });
    $("#calcWindow").window("open");
}
// 计算基准和转换系数
function calcAdjAndScale() {
    if (PageAin.dgData.rows.length == 0) return;
    let dgId = "PageAin_dg";
    let vals = [];
    for (let i = 0; i < 4; i++) {
        vals.push(parseFloat($("#ainWin_r" + i).val()));
    }
    let row = $("#" + dgId).datagrid("getSelected");
    if (!row) row = PageAin.dgData.rows[0];
    if (vals[0] == vals[1]) {
        row.adjValue = 0;
        row.scaleValue = 1;
    } else {
        row.scaleValue = (vals[3] - vals[2]) / (vals[1] - vals[0]);
        row.adjValue = vals[0] * row.scaleValue - vals[2];
    }
    DatagridTool.updateData(dgId, true);
    //标记为修改
    DatagridTool.addToUpdatedRows(dgId, row);
}